/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * <p>
 * PmCustomerBillController
 * 客户开票档案
 * <p>
 * 修改纪录
 * 2020-05-12 版本：1.0 wushenghong 创建。
 *
 * @version 版本：1.0
 * @author 作者：wushenghong
 * 创建日期：2020-05-12
 */

package com.topscomm.pm.controller;

import com.topscomm.pm.controller.auto.PmCustomerBillControllerAuto;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.ITapService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: wushenghong
 * @date: 2020-05-12
 * @description:客户开票档案Controller
 * @modify:
 */
@RestController
@RequestMapping("pm/customerBill")
public class PmCustomerBillController extends PmCustomerBillControllerAuto implements IPmCustomerBillController {
    /**
     * @param paraMap
     * @return
     * @author: wushenghong
     * @date: 2020-05-12
     * @description:转换出查询SQL语句
     * @modify:
     */
    @Override
    protected String spellListSql(Map<String, Object> paraMap) {
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" 1=1 ");
        if (paraMap.containsKey("qry_companyname")) {
            String value = paraMap.get("qry_companyname").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and companyname like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_receiver")) {
            String value = paraMap.get("qry_receiver").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and receiver like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_username")) {
            String value = paraMap.get("qry_username").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and username like '%{0}%' ", value);
        }
        // 根据客户档案查询开票档案
        if (paraMap.containsKey("qry_customerid")) {
            String value = paraMap.get("qry_customerid").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and customerid = '{0}' ", value);
        }
        if (paraMap.containsKey("qry_customername")) {
            String value = paraMap.get("qry_customername").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and companyname = '{0}' ", value);
        }
        return sql.toString();
    }

    /**
     * @param paraMap
     * @return
     * @author: wushenghong
     * @date: 2020-05-12
     * @description:转换出查询SQL语句
     * @modify:
     */
    @Override
    @GetMapping(value = "/refList")
    public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
        super.refList(paraMap);
        try {
            paraMap = BaseUtil.decodeSecureMap(paraMap);
            Query query = new Query(paraMap);
            // 添加有效过滤
            String sqlWhere =this.spellRefListSql(paraMap) + "and enabled = 1";
            int totalCount = ((ITapService)this.service).getCount(sqlWhere);
            List<Map<String, Object>> tempList = null;
            if (query.getPageSize() > 0) {
                tempList = ((ITapService)this.service).queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(), query.getSidx(), query.getSord());
            } else {
                tempList = ((ITapService)this.service).queryMapByWhere(sqlWhere);
            }

            ((ITapService)this.service).setDispFields(tempList);
            Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
            return ResponseResult.ok(page);
        } catch (Exception var7) {
            return this.handleControllerException(var7);
        }
    }

    /**
     * @param dataMap
     * @param paraMap
     * @throws Exception
     * @description: 提交审批前
     * @author: wushenghong
     * @date: 2020-05-12
     * @modify:
     */
    @Override
    protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
        super.beforeSubmit(dataMap, paraMap);
        StringBufferProxy mailtitle = new StringBufferProxy();
        mailtitle.appendSingle("{0}{1}于{2}提交客户开票档案申请,单据编号{3}! ", dataMap.get("dispdeptcode"), dataMap.get("username"), DateUtil.dateToString(new Date(), "yyyy-MM-dd"), dataMap.get("docno"));
        dataMap.put("maintitle", mailtitle.toString());
    }
}

